/***************************************************************************
 创建者: 华磊
 开始时间: 2017.6.13
 copyright: (C) 同川科技
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 (1) 华磊　2017.6.13
 ***************************************************************************
 *  @file DataAnalysizerDouble.cpp
 *  功能: vector数据的统计分析。可以求速度，加速度
 *
 *                                                                         *
 ***************************************************************************/
#include "dataanalysizerdouble.h"
#include "qdebug.h"
#include "stdio.h"
#include <iostream>
#include "unistd.h"

DataAnalysizerDouble::DataAnalysizerDouble(QString _dataComent, int _isPrint, int _printInverval
                               , int _dataRow, int  _dataNumberMax, int _dataOverFlowMethod)
{
    dataComment=_dataComent;
    isPrint=_isPrint;
    printInterval=_printInverval;
    dataRow=_dataRow;
    dataNumberMax=_dataNumberMax;
    dataOverFlowMethod=_dataOverFlowMethod;
    resetDataForAnalysize();

}
int DataAnalysizerDouble::recordDataForAnalysize( std::vector<double> _dataForAnalysizer)
{
    //数据条数处理逻辑
     if(currentDataNumber==dataNumberMax && 0==dataOverFlowMethod)
     {
         return -1;
     }
     else if(currentDataNumber==dataNumberMax && 1==dataOverFlowMethod)
     {
         resetDataForAnalysize();
     }
     else if(currentDataNumber>=dataNumberMax && 2==dataOverFlowMethod)
     {
         dataQueue.pop();
         dataIncrementQueue.pop();
         dataIncrementIncrementQueue.pop();
     }
     //打印处理逻辑
     if(tmpRecordDataNumber==printInterval)
     {
         tmpRecordDataNumber=0;
         if(1==isPrint)
         {
            qDebug()<<dataComment;
             std::vector<double> dataAverageTmp=getDataAverage();
             std::cout << "\nAverage-------------------------\n" <<std::endl ;
             for(int i=0;i<dataRow;i++)
             {
                 std::cout << "/   " <<dataAverageTmp[i] <<".   " ;
             }
             std::cout <<"\nMax-------------------------\n"<<std::endl;
             for(int i=0;i<dataRow;i++)
             {
                 std::cout << "/   "<<dataMax[i] <<".   " ;
             }
             std::cout <<"\nMin-------------------------\n"<<std::endl;
             for(int i=0;i<dataRow;i++)
             {
                 std::cout << "/   "<<dataMin[i] <<".   " ;
             }
             std::cout <<"\nMaxAbs-------------------------\n"<<std::endl;
             for(int i=0;i<dataRow;i++)
             {
                 std::cout << "/   "<<dataMaxAbs[i] <<".   " ;
             }
             std::cout <<"\nMinAbs-------------------------\n"<<std::endl;
             for(int i=0;i<dataRow;i++)
             {
                 std::cout << "/   "<<dataMinAbs[i]  <<".   ";
             }
             std::cout << "\n  ";
         }


     }
     tmpRecordDataNumber+=1;

    //更新旧data
   if(0==dataForAnalysizer.size())
    {
        dataForAnalysizer.resize(dataRow);
        dataForAnalysizer=_dataForAnalysizer;
    }
   else if(0<dataForAnalysizer.size() && 0==dataForAnalysizerOld.size() )
    {
        dataForAnalysizerOld.resize(dataRow);
        dataForAnalysizerOld=dataForAnalysizer;
        dataForAnalysizer=_dataForAnalysizer;
    }
    else if(0<dataForAnalysizer.size() && 0<dataForAnalysizerOld.size() )
    {
         dataForAnalysizerOld=dataForAnalysizer;
         dataForAnalysizer=_dataForAnalysizer;
    }

    //更新dataIncrement
    if( 0==dataIncrement.size() && 0<dataForAnalysizerOld.size())
    {
        dataIncrement.resize(dataRow);
        for(int i=0;i<dataRow;i++)
        {
            dataIncrement[i]=dataForAnalysizer[i]-dataForAnalysizerOld[i];
        }
    }
   else if(0<dataIncrement.size() && 0== dataIncrementOld.size())
    {
        dataIncrementOld.resize(dataRow);
         dataIncrementOld=dataIncrement;
         for(int i=0;i<dataRow;i++)
         {
             dataIncrement[i]=dataForAnalysizer[i]-dataForAnalysizerOld[i];
         }
    }
   else if(0<dataIncrement.size() && 0<dataIncrementOld.size() )
    {
         dataIncrementOld=dataIncrement;
         for(int i=0;i<dataRow;i++)
         {
             dataIncrement[i]=dataForAnalysizer[i]-dataForAnalysizerOld[i];
         }
    }

     //更新dataIncrementIncrement
    if(0<dataIncrementOld.size() && 0<dataIncrement.size())
    {
        if(0==dataIncrementIncrement.size())
        {
            dataIncrementIncrement.resize(dataRow);
        }
        for(int i=0;i<dataRow;i++)
        {
            dataIncrementIncrement[i]=dataIncrement[i]-dataIncrementOld[i];
        }
    }

    static int __count=0;
     if( __count <100 )
         {
           __count++;
            qDebug( )<<__count<<"dataIncrementQueue.size"<<dataIncrementQueue.size()
                    <<"dataIncrement.size()"<<dataIncrement.size()
                    <<"dataIncrementIncrementQueue.size()"<<dataIncrementIncrementQueue.size()
                   <<"dataIncrementIncrement.size()"<<dataIncrementIncrement.size();
         }

    //queue处理
   dataQueue.push(_dataForAnalysizer);
   if(0<dataIncrement.size())
   {
       dataIncrementQueue.push(dataIncrement);
   }
   if(0<dataIncrementIncrement.size())
   {
       dataIncrementIncrementQueue.push(dataIncrementIncrement);
   }


    //计算dataMax
    for(int i=0;i<dataRow;i++)
    {
        if(0<dataForAnalysizer.size())
        {
            if(dataForAnalysizer[i]>dataMax[i])
            {
                dataMax[i]=dataForAnalysizer[i];

            }
        }
    }

    //计算dataMin
    if(0!=dataMin.size())
    {
        for(int i=0;i<dataRow;i++)
        {
            if(0<dataForAnalysizer.size())
            {
                if(dataForAnalysizer[i]<dataMin[i])
                {
                    dataMin[i]=dataForAnalysizer[i];

                }
            }
        }
    }
    else
    {
        dataMin.resize(dataRow);
        dataMin=dataForAnalysizer;
    }

    //计算dataMaxAbs
    for(int i=0;i<dataRow;i++)
    {
        if(0<dataForAnalysizer.size())
        {
            if(abs(dataForAnalysizer[i])>dataMaxAbs[i])
            {
                dataMaxAbs[i]=abs(dataForAnalysizer[i]);
            }
        }
    }

    //计算dataMinAbs
    if(0!=dataMinAbs.size())
    {
        for(int i=0;i<dataRow;i++)
        {
            if(0<dataForAnalysizer.size())
            {
                if(abs(dataForAnalysizer[i])<dataMinAbs[i])
                {
                    dataMinAbs[i]=abs(dataForAnalysizer[i]);

                }
            }
        }
    }
    else
    {
        dataMinAbs.resize(dataRow);
        dataMinAbs=dataForAnalysizer;
    }

    currentDataNumber+=1;


    return 1;
}

int DataAnalysizerDouble::resetDataForAnalysize()
{
    dataForAnalysizer.resize(0);
    dataForAnalysizerOld.resize(0);
    dataMax.resize(dataRow);
    dataMin.resize(0);
    dataAverage.resize(dataRow);
    dataMaxAbs.resize(dataRow);
    dataMinAbs.resize(0);
    dataAverageAbs.resize(dataRow);
    dataIncrement.resize(0);
    dataIncrementOld.resize(0);
    dataIncrementIncrement.resize(0);
    int  queueSize=dataQueue.size();
    for(int i=0;i<queueSize;i++)
    {
      dataQueue.pop();
    }
    queueSize=dataIncrementQueue.size();
    for(int i=0;i<queueSize;i++)
    {
      dataIncrementQueue.pop();
    }
    queueSize=dataIncrementIncrementQueue.size();
    for(int i=0;i<queueSize;i++)
    {
      dataIncrementIncrementQueue.pop();
    }


    for(int  i=0;i<dataRow;i++)
    {
        dataMax[i]=0;
    }

    for(int  i=0;i<dataRow;i++)
    {
        dataAverage[i]=0;
    }
    for(int  i=0;i<dataRow;i++)
    {
        dataMaxAbs[i]=0;
    }
    for(int  i=0;i<dataRow;i++)
    {
        dataAverageAbs[i]=0;
    }
    currentDataNumber=0;
    tmpRecordDataNumber=0;
    return 1;
}

std::vector<double> DataAnalysizerDouble::getDataMax()
{
    return dataMax;
}

std::vector<double> DataAnalysizerDouble::getDataMin()
{
    return dataMin;

}

std::vector<double> DataAnalysizerDouble::getDataAverage()
{
    std::vector<double> tmpSum;
    tmpSum.resize(dataRow);
    std::vector<double> tmpData;
    tmpSum.resize(dataRow);
    std::queue< std::vector<double> > tmpDataQueue;
    tmpDataQueue=dataQueue;
    for(int i=0;i<dataRow;i++)
    {
    tmpSum[i]=0;
    }
    int qsize=tmpDataQueue.size();
    for(int j=0;j<qsize;j++)
    {
       tmpData=tmpDataQueue.front();
       tmpDataQueue.pop();
        for(int i=0;i<dataRow;i++)
        {
        tmpSum[i]+=tmpData[i];
        }
    }
    for(int i=0;i<dataRow;i++)
    {
    tmpSum[i]=tmpSum[i]/dataQueue.size();
    }
    return tmpSum;

}

std::vector<double> DataAnalysizerDouble::getDataMaxAbs()
{
    return dataMaxAbs;

}

std::vector<double> DataAnalysizerDouble::getDataMinAbs()
{
    return dataMinAbs;

}

std::vector<double> DataAnalysizerDouble::getDataAverageAbs()
{
    return dataAverageAbs;

}

std::vector<double> DataAnalysizerDouble::getDataIncrement()
{


    return dataIncrement;
}

std::vector<double> DataAnalysizerDouble::getDataIncrementIncrement()
{
    return dataIncrementIncrement;

}

std::queue<std::vector<double> > DataAnalysizerDouble::getDataQueue()
{
    return dataQueue;
}

int DataAnalysizerDouble::writeDataQueueToFile(QString newFileName)
{

         //文件初始化
        char*  ch;
        QByteArray ba = newFileName.toLatin1();
        ch=ba.data();
        qDebug()<<"开启:将DataQueue（double)的数据写入到文件功能!";
        FILE* fileObject ;
        fileObject = fopen( ch ,"w");
        if(fileObject!=NULL)
        {
            fclose(fileObject);
        }
        if ((fileObject=fopen(ch,"a+"))==NULL)//
        {
             qDebug()<< "文件打开失败:" <<ch;
             return -1;
        }

        if(dataIncrementIncrementQueue.size()<1)
        {
            fclose(fileObject);
            qDebug()<< "queue里面没有数据了，无法保存到文件";
            return -1;
        }

        //文件内容填充
        std::queue< std::vector<double> > tmpDataQueue;
        std::queue< std::vector<double> > tmpDataIncrementQueue;
        std::queue< std::vector<double> > tmpDataIncrementIncrementQueue;
        tmpDataQueue=dataQueue;
        tmpDataIncrementQueue=dataIncrementQueue;
        tmpDataIncrementIncrementQueue=dataIncrementIncrementQueue;

        std::vector<double> tmpData;
        std::vector<double> tmpDataIncrement;
        std::vector<double> tmpDataIncrementIncrement;

        //第一条数据
            tmpData=tmpDataQueue.front();
            tmpDataQueue.pop();
            for(int i=0;i<dataRow;i++)
            {
                fprintf( fileObject, "%f , " , tmpData[i] );
            }
            fprintf( fileObject, "\n"  );

        //第二条数据
            tmpData=tmpDataQueue.front();
            tmpDataQueue.pop();
            tmpDataIncrement=tmpDataIncrementQueue.front();
            tmpDataIncrementQueue.pop();
            for(int i=0;i<dataRow;i++)
            {
                fprintf( fileObject, "%f , " , tmpData[i] );
            }
            for(int i=0;i<dataRow;i++)
            {
                fprintf( fileObject, "%f , " , tmpDataIncrement[i] );
            }
            fprintf( fileObject, "\n"  );

         //剩余数据
            int tmpNumber=tmpDataQueue.size();
            int tmpNumber2=tmpDataIncrementQueue.size();
            int tmpNumber3=tmpDataIncrementIncrementQueue.size();
            for(int j=0;j<tmpNumber;j++)
            {
                tmpData=tmpDataQueue.front();
                tmpDataQueue.pop();
                tmpDataIncrement=tmpDataIncrementQueue.front();
                tmpDataIncrementQueue.pop();
                tmpDataIncrementIncrement=tmpDataIncrementIncrementQueue.front();
                tmpDataIncrementIncrementQueue.pop();
                for(int i=0;i<dataRow;i++)
                {
                    fprintf( fileObject, "%f , " , tmpData[i] );
                }
                for(int i=0;i<dataRow;i++)
                {
                    fprintf( fileObject, "%f , " , tmpDataIncrement[i] );
                }
                for(int i=0;i<dataRow;i++)
                {
                    fprintf( fileObject, "%f , " , tmpDataIncrementIncrement[i] );
                }
                fprintf( fileObject, "\n"  );
            }

        fflush( fileObject);
        fclose(fileObject);
        return  1;

}
